package github.kawaiior.yuanchuxiuzhen.common.item.misc;

import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.core.init.ItemInit;
import github.kawaiior.yuanchuxiuzhen.core.init.itemgroup.YcxzGroup;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Food;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.util.IItemProvider;
import net.minecraft.world.Explosion;
import net.minecraft.world.World;
import net.minecraftforge.common.util.LazyOptional;

public class ManaStoneItem extends Item {

    private static final Food food = (new Food.Builder())
            .saturation(0)  // 饱腹度
            .hunger(0) // 鸡腿
            .setAlwaysEdible().build();

    private int level = 1;

    public ManaStoneItem(int level) {
        super(new Properties().group(YcxzGroup.YCXZ_GROUP).food(food));
        this.level = level;
    }

//    // 改成右键使用
//    public ManaStoneItem(int level) {
//        super(new Properties().group(YcxzGroup.YCXZ_GROUP));
//        this.level = level;
//    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getExperience(){
        return 5 * (int) Math.pow(2, this.level-1);
    }

    public float getMana(){
        return (float) Math.pow(2, this.level-1) * 10F;
    }

    private void onUsed(PlayerEntity player, World world){
        LazyOptional<ManaPower> capability = player.getCapability(ModCapability.MANA_POWER);
        capability.ifPresent((power)->{
            power.addExperience(getExperience(), player.world, player);
            power.addMana(getMana(), player, world);

            int cut = this.level - power.getLevel();
            if (cut >= 3){
                world.createExplosion(player, player.getPosX(), player.getPosY()+2F, player.getPosZ(),
                        6F, false, Explosion.Mode.NONE);
                float damage;
                if (cut==3){
                    damage = 1F;
                }else {
                    damage = (float) (5F * Math.pow(4D, cut-4));
                }
                player.attackEntityFrom(DamageSource.causeIndirectMagicDamage(player, player), damage);
            }
        });
    }

//    @Override
//    public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
//        if (!player.world.isRemote){
//            onUsed(player, world);
//        }
//        return super.onItemRightClick(world, player, hand);
//    }

    @Override
    public ItemStack onItemUseFinish(ItemStack stack, World world, LivingEntity entityLiving) {
        if (entityLiving instanceof PlayerEntity && !entityLiving.world.isRemote){
            onUsed((PlayerEntity) entityLiving, world);
        }
        return super.onItemUseFinish(stack, world, entityLiving);
    }

    public static IItemProvider getManaStone(int level){
        switch (level) {
            case 1:
                return ItemInit.MANA_STONE_ITEM_LEVEL_1.get();
            case 2:
                return ItemInit.MANA_STONE_ITEM_LEVEL_2.get();
            case 3:
                return ItemInit.MANA_STONE_ITEM_LEVEL_3.get();
            case 4:
                return ItemInit.MANA_STONE_ITEM_LEVEL_4.get();
            case 5:
                return ItemInit.MANA_STONE_ITEM_LEVEL_5.get();
            case 6:
                return ItemInit.MANA_STONE_ITEM_LEVEL_6.get();
            case 7:
                return ItemInit.MANA_STONE_ITEM_LEVEL_7.get();
            case 8:
                return ItemInit.MANA_STONE_ITEM_LEVEL_8.get();
            case 9:
                return ItemInit.MANA_STONE_ITEM_LEVEL_9.get();
        }
        return ItemInit.MANA_STONE_ITEM_LEVEL_1.get();
    }

}
